<?php 

class suipaiModel extends appModel
{
	
	public function check_is_praise($wxid,$pid)
	{
		$sql = "select count(*) from ".$this->table('praise')." where wxid='".$wxid."' and pid='".$pid."'";
		return $this->getOne($sql);
	}
	
	public function check_is_picture($wxid)
	{
		$sql = "select count(*) from ".$this->table('picture')." where wxid='".$wxid."'";
		return $this->getOne($sql);
	}
	
	public function check_is_vote_praise($wxid)
	{
		$sql = "select count(*) from ".$this->table('vote_praise')." where wxid='".$wxid."'";
		return $this->getOne($sql);
	}
	
	public function check_is_exist($wxid)
	{
		$sql = "select count(*) from ".$this->table('suipai')." where wxid=".$wxid." and created>".strtotime(date('Y-m-d'));
		return $this->getOne($sql);
	}
	
	public function check_is_vote($info)
	{
		$sql = "select count(*) from ".$this->table('vote_record')." where wxid=".$info['wxid']." and vid=".$info['vid']." and oid=".$info['oid'];
		return $this->getOne($sql);
	}
	
	public function insert_info($info)
	{
		$data = yf_set_insert($info);
		$sql = "insert into ".$this->table('suipai')." (".$data['key'].") values (".$data['val'].")";
		return $this->query($sql);
	}
	
	
	public function insert_vote($info)
	{
		$data = yf_set_insert($info);
		$sql = "insert into ".$this->table('vote_record')." (".$data['key'].") values (".$data['val'].")";
		$this->query($sql);
		$sql = " update ".$this->table('vote')." set joinnums = joinnums + 1 where id = ".$info['vid'];
		$this->query($sql);
		
		$sql = " update ".$this->table('option')." set playnums = playnums + 1 where id = ".$info['oid'];
		
		return $this->query($sql);
	}
	
	public function insert_record($info,$table)
	{
		$data = yf_set_insert($info);
		$sql = "insert into ".$this->table($table)." (".$data['key'].") values (".$data['val'].")";
		
		return $this->query($sql);
	}
	
	//点赞
	public function insert_praise($info)
	{
		$back = $this->insert_record($info, "praise");
		
		
		if($back)
		{
			$this->setInc("praise", 1, "picture", $info['pid']);
		}
		
		return $back;
	}
	
	//投票
	public function insert_vote_praise($info,$table)
	{
		$back = $this->insert_record($info, $table);
	
	
		if($back)
		{
			$this->setInc("vote", 1, "picture", $info['pid']);
		}
	
		return $back;
	}
	
	public function setInc($field,$step,$table,$id)
	{
		
		
		$sql = " update ".$this->table($table)." set ".$field." = ".$field." + ".$step." where id = '".$id."'";
	
		return $this->query($sql);
	}
	
	public function getVote($id)
	{
		$time = yf_time();
		$sql = "SELECT * FROM ".$this->table('vote')."  WHERE   status = 1 and id= ".$id." and dotime1 <=".$time." and dotime2 >=".$time;
		$result = $this->getRow($sql);
		if($result)
		{
			$sql = "SELECT * FROM ".$this->table('option')."  WHERE vid = '".$result['id']."'";
			$option = $this->getAll($sql);
			$result['option'] = $option;
		}
		
		return $result;
	}
	
	public function getCount()
	{
		
		$sql = "SELECT count(*) FROM ".$this->table('picture')."  WHERE  status = 1 ";
		$result = $this->getOne($sql);
		
	
		return $result;
	}
	
	public function get_list()
	{
		$sql = "SELECT p1.*,p2.nickname,p2.openid FROM ".$this->table('picture')." as p1 ".
				"LEFT JOIN ".$this->table('wxusers')." as p2 on p1.wxid = p2.wxid WHERE p1.status =1  ORDER BY created ";
	
		$result = $this->getAll($sql);
		
		
		
		foreach ($result as $k => $v)
		{
			
			$v['pic'] = "http://".$_SERVER['HTTP_HOST']."/ws/".$v['pic'];
			
			$result[$k]= $v;
		}
		
		
		
	
		return $result;
	}
	
	
	public function get_list_parise()
	{
		$sql = "SELECT p1.*,p2.nickname,p2.openid FROM ".$this->table('picture')." as p1 ".
				"LEFT JOIN ".$this->table('wxusers')." as p2 on p1.wxid = p2.wxid WHERE p1.status =1  ORDER BY praise desc ,created ";
	
		$result = $this->getAll($sql);
	
	
	
		foreach ($result as $k => $v)
		{
				
			$v['pic'] = "http://".$_SERVER['HTTP_HOST']."/ws/".$v['pic'];
				
			$result[$k]= $v;
		}
	
	
	
	
		return $result;
	}
	
}

?>